package com.lgd.factoryattendance.Broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.util.Log;

import com.lgd.factoryattendance.Activity.StartActivity;
import com.lgd.factoryattendance.Utils.ToastUtils;

import java.io.IOException;


/**
 * @Author lgd
 * @create 2024/5/30 9:09
 */
public class MyReceiver extends BroadcastReceiver {

    final static String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        // APP  自启
        if(intent.getAction().equals(ACTION)){
            Intent openIntent = new Intent(context, StartActivity.class);
            openIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    context.startActivity(openIntent);
                }
            },1000);
        }

        if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                Log.d("连接状态", "连接到WIFI " + wifiInfo);
                if (!wifiInfo.getSSID().equals("<unknown ssid>")){
                    ToastUtils.successShowLong(context,"连接到WiFi"+wifiInfo.getSSID());
                }
            }
        }
        if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
            int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
            if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
                Log.d("连接状态", "WIFI关闭");
                ToastUtils.successShowLong(context,"WiFi关闭");
            }
            if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
                Log.d("连接状态", "WIFI开启");
                ToastUtils.successShowLong(context,"WiFi开启");
            }
        }

    }
    public static class NetworkDiagnostics {
        //方案一
        public static void start(final Context context){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 -w 100 www.baidu.com");
                        int status = process.waitFor(); //如果status==0则表示网络可用，其中参数-c 1是指ping的次数为1次，-w是指超时时间单位为s
                        if (status == 0){     //网络能通
                            Log.d("wangrui","网络分析方案1=true");
                            System.out.println("-----可用------");
                        }else{
                            Log.d("wangrui","网络分析方案1=false");
                        }
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        //方案二
        public static boolean isNetworkAvailable(Context context) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager == null) {
                return false;
            } else {
                // 获取NetworkInfo对象
                NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
                if (networkInfo != null && networkInfo.length > 0) {
                    for (int i = 0; i < networkInfo.length; i++) {
                        // 判断当前网络状态是否为连接状态
                        if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }

}
